package cz.zcu.fav.rat.rest.factory;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Multimap;

import cz.zcu.fav.rat.beans.Period;
import cz.zcu.fav.rat.beans.Object;
import cz.zcu.fav.rat.beans.Reservation;
import cz.zcu.fav.rat.beans.User;
import cz.zcu.fav.rat.dataaccess.service.ObjectService;
import cz.zcu.fav.rat.dataaccess.service.UserService;
import cz.zcu.fav.rat.rest.beans.ReservationMapping;
import cz.zcu.fav.rat.rest.beans.ReservationsMapping;
import cz.zcu.fav.rat.rest.errorhandling.UnknownResourceException;

@Component
public class ReservationsMappingFactory
		extends
		AbstractMappingFactory<ReservationsMapping, Map<Reservation, Collection<Period>>>
{

	@Autowired
	ReservationMappingFactory reservationFactory;

	@Override
	public ReservationsMapping getObject()
	{
		return new ReservationsMapping();
	}

	@Override
	public Class<?> getObjectType()
	{
		return ReservationsMapping.class;
	}

	@Override
	public ReservationsMapping getMappedObject(
			Map<Reservation, Collection<Period>> reservations)
	{
		ReservationsMapping reservationsMapping = getObject();
		List<ReservationMapping> mappedReservations = new ArrayList<ReservationMapping>();
		for (Entry<Reservation, Collection<Period>> entry : reservations
				.entrySet())
		{
			mappedReservations.add(reservationFactory.getMappedObject(entry));
		}
		reservationsMapping.setReservations(mappedReservations);

		return reservationsMapping;
	}

	@Override
	public Map<Reservation, Collection<Period>> getRemappedObject(
			ReservationsMapping object, boolean ignoreId)
	{
//		Multimap<Reservation, Period> multiMap = ArrayListMultimap.create();
		Map<Reservation, Collection<Period>> map = new HashMap<Reservation, Collection<Period>>();	
		List<ReservationMapping> reservations = object.getReservations();
		for (ReservationMapping resMapping : reservations)
		{
			Entry<Reservation, Collection<Period>> remappedEntry = reservationFactory
					.getRemappedObject(resMapping, ignoreId);
			map.put(remappedEntry.getKey(), remappedEntry.getValue());
			
		}
		return map;
	}
}
